#!/bin/bash


#
#   Developer:      Are Hansen
#   Date:           2014, May 5
#   Version:        0.1.2
#
#   Usage:
#   This script is executed by the debconf installer and takes care of configuration, package
#   installation, updating and upgrading of Bifrozt during installation.
#
#   Copyright (c) 2014, Are Hansen - Honeypot Development
#
#   All rights reserved.
#
#   Redistribution and use in source and binary forms, with or without modification, are permitted
#   provided that the following conditions are met:
#
#   1. Redistributions of source code must retain the above copyright notice, this list of
#   conditions and the following disclaimer.
#
#   2. Redistributions in binary form must reproduce the above copyright notice, this list of
#   conditions and the following disclaimer in the documentation and/or other materials provided
#   with the distribution.
#
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND AN EXPRESS OR
#   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
#   FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
#   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
#   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
#   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
#   POSSIBILITY OF SUCH DAMAGE.
#
# ---------- The Bifrozt Honeypot Project 2014 ---------- #


unset DEBCONF_REDIR
unset DEBCONF_FRONTEND
unset DEBIAN_HAS_FRONTEND
unset DEBIAN_FRONTEND


#
# - Absolute path to files and directories
#
declare Script="Bifrozt-installer"
declare honeyd="/usr/share/honeyd/scripts"
declare Slog="/var/log/Bifrozt_Install.log"
declare bifrozt_scripts="/root/bifrozt/bifrozt.script"
declare bifrozt_configs="/root/bifrozt/bifrozt.configs"
declare service_configs="/root/bifrozt/service.configs"
declare service_scripts="/root/bifrozt/service.scripts"

#
# - Absolute path to executables
#
declare -rx chmod="/bin/chmod"
declare -rx date="/bin/date"
declare -rx echo="/bin/echo"
declare -rx mkdir="/bin/mkdir"
declare -rx mv="/bin/mv"
declare -rx rm="/bin/rm"
declare -rx aptget="/usr/bin/apt-get"
declare -rx git="/usr/bin/git"
declare -rx touch="/usr/bin/touch"


function update_system()
{
    #
    #   Install the lastest updates for Ubuntu server 12.04 LTS
    #
    $aptget update &>/dev/null

    $aptget upgrade -y &>/dev/null
}


function install_bifrozt_pkgs()
{
    #
    #   Install the official deb packages required by Bifrozt.
    #
    $aptget install git isc-dhcp-server htop slurm python-twisted python-mysqldb python-paramiko \
    geoip-bin geoip-database python-geoip honeyd iisemulator ttf-baekmuk ttf-arphic-gbsn00lp \
    ttf-arphic-bsmi00lp ttf-arphic-gkai00mp ttf-arphic-bkai00mp librrds-perl gawk -y
}


function git_bifrozt()
{
    #
    #   Download the latest version of the Bifrozt packages.
    #
    cd /root

    $git clone https://code.google.com/p/bifrozt/ bifrozt
}


function bifrozt_configs()
{
    #
    #   Install the Bifrozt system files.
    #
    $mv $bifrozt_configs/bifrozt-sysctl.conf /etc/sysctl.conf

    $mv $bifrozt_configs/bifrozt-tables /etc/network/iptables

    $mv $bifrozt_configs/bifrozt-tables6 /etc/network/iptables6

    $mv $bifrozt_configs/bifrozt-dhcpd.conf /etc/dhcp/dhcpd.conf

    $mv $bifrozt_configs/bifrozt-sshd_config /etc/ssh/sshd_config

    $mv $bifrozt_configs/bifrozt-interfaces /etc/network/interfaces

    $mv $bifrozt_configs/bifrozt-motd-00-header /etc/update-motd.d/00-header

    $mv $bifrozt_configs/bifrozt-10-help-text /etc/update-motd.d/10-help-text

    $mv $bifrozt_configs/bifrozt-13-iptables.conf /etc/rsyslog.d/13-iptables.conf

    $mv $bifrozt_configs/bifrozt-fw-rotate /etc/logrotate.d/iptables
}


function bifrozt_scripts()
{
    #
    #   Install the Bifrozt speciffic scripts.
    #
    $mv $bifrozt_scripts/bifrozt_stats.py /usr/local/bin/bifrozt_stats

    $mv $bifrozt_scripts/bifrozt-hrrotate /etc/cron.hourly/logrotate

    $mv $bifrozt_scriptss/honsshwrap.sh /usr/local/bin/honsshctrl.sh
}


function service_configs()
{
    #
    #   Install the honeypot related configuration files.
    #
    $mv $service_configs/bifrozt-hd.conf /etc/honeypot/honeyd.conf
}


function service_scripts()
{
    #
    #   Install the honeypot related scripts.
    #
    $mv $service_scripts/ssh.sh $honeyd/unix/linux/suse8.0/ssh.sh

    if [ ! -d $honeyd/unix/OSX ]
    then
        $mkdir -p $honeyd/unix/OSX
    fi

    $mv $service_scripts/osx.sh $honeyd/unix/OSX/osx_ssh.sh

    $mv $service_scripts/base.sh $honeyd/misc/base.sh

    $mv $service_scripts/exchange-pop3.sh $honeyd/win32/win2k/exchange-pop3.sh
}


function git_honssh()
{
    #
    #   Install HonSSH.
    #
    $git clone https://code.google.com/p/honssh/ /opt/honssh
}


function clean_up()
{
    #
    #   Preform the last configurations, set permissions and remove unwanted files.
    #
    $echo 'INTERFACES="eth1"' > /etc/default/isc-dhcp-server

    $touch /var/log/honeypot/honeyd.log

    $chmod 0777 /var/log/honeypot

    $chmod 0755 /etc/update-motd.d/00-header

    $chmod 0755 /etc/update-motd.d/10-help-text

    $chmod 0755 /etc/cron.hourly/logrotate

    $chmod 0755 $honeyd/unix/linux/suse8.0/ssh.sh

    $chmod 0755 $honeyd/unix/OSX/osx_ssh.sh

    $chmod 0755 $honeyd/misc/base.sh

    $chmod 0755 /usr/local/bin/bifrozt_stats
    
    $chmod 0755 /usr/local/bin/honsshctrl.sh

    $chmod 0666 /var/log/honeypot/honeyd.log

    $chmod 0644 /etc/honeypot/honeyd.conf

    $chmod 0644  /etc/logrotate.d/iptables

    $chmod 0644 $Slog

    $chmod 0644 $Slog

    $rm /etc/update-motd.d/50-landscape-sysinfo

    $rm -r /root/bifrozt
}


#
#   Execute all other functions. Send all error messages from the functions to the error log.
#
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt installation has started" >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Updating the system." >> $Slog
update_system
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: System up to date." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing Bifrozt base." >> $Slog
install_bifrozt_pkgs 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt base installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Getting latest version of Bifrozt." >> $Slog
git_bifrozt 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Latest Bifrozt version accuired." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing configuration files." >> $Slog
bifrozt_configs 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Configuration files installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing Bifrozt scripts." >> $Slog
bifrozt_scripts 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt scripts installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing service configurations." >> $Slog
service_configs 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Service configuration installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing service scripts." >> $Slog
service_scripts 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Service scripts installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Installing HonSSH." >> $Slog
git_honssh 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: HonSSH installed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Running post-install actions." >> $Slog
clean_up 2>> $Slog
$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Post-install actions completed." >> $Slog


$echo "$($date +"%Y  %b %d - %T") $Script[$$]: Bifrozt installation is complete" >> $Slog


exit 0
